package com.yangxd.response;

//import com.oracle.webservices.internal.api.databinding.DatabindingMode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回结果
 * @Author: yangxudong
 * @Date: 2021/2/28 20:55
 * @Version 1.0
 */
@Data
public class Result {

    @ApiModelProperty(value = "是否成功")
    private Boolean isSuccess;

    @ApiModelProperty(value = "返回状态码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回的数据")
    private Map<String,Object> Data = new HashMap<>();

    /**
     * 构造方法私有化，里面的方法都是
     * 静态方法，达到保护属性的作用
     */
    private Result() {

    }

    /**
     * 使用链式编程
     */

    /**
     * 成功时调用
     * @return
     */
    public static Result ok() {
        Result result = new Result();
        result.setIsSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * 错误时返回
     * @return
     */
    public static Result error() {
        Result result =  new Result();
        result.setIsSuccess(false);
        result.setCode(ResultCode.COMMON_FAIL.getCode());
        result.setMessage(ResultCode.COMMON_FAIL.getMessage());
        return result;
    }

    public static Result error(ResultCode resultCode){
        Result result = new Result();
        result.setIsSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }


    /**
     * 自定义返回成功与否
     * @param isSuccess
     * @return
     */
    public Result success(Boolean isSuccess) {
        this.setIsSuccess(isSuccess);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result data(String key,Object value) {
        this.Data.put(key, value);
        return this;
    }

}
